package com.societyGames.codeAlong.state.exerciseSelect
{
import com.societyGames.codeAlong.state.ChangeStateEvent;
import com.societyGames.codeAlong.state.IState;
import com.societyGames.codeAlong.state.StateEnum;
import com.societyGames.codeAlong.state.codeExercise.model.ModuleData;
import com.societyGames.codeAlong.state.codeExercise.view.EditorCloseEnum;
import com.societyGames.codeAlong.state.codeExercise.view.EditorCloseEvent;
import com.societyGames.codeAlong.state.codeExercise.view.EditorView;
import com.societyGames.codeAlong.user.PersistentUserData;
import com.societyGames.system.EventObject;

import flash.display.DisplayObject;
import flash.events.EventDispatcher;

public class ExerciseSelectState extends EventDispatcher implements IState
{
  private var _changeStateEventObject:EventObject;
  private var _moduleDatas:Vector.<ModuleData>;
  private var _editorView:EditorView;

  public function ExerciseSelectState(userData:PersistentUserData, moduleDatas:Vector.<ModuleData>)
  {
    this._changeStateEventObject = new EventObject(this, "CHANGE_STATE");
    this._moduleDatas = moduleDatas;
    this._editorView = new EditorView(userData, this._moduleDatas);
    this._editorView.setEditorModel(null);
    this._editorView.closeEventObject.addEventListener(editorCompleteHandler);
  }

  //IState-->
  public function get displayObject():DisplayObject
  {
    return this._editorView;
  }

  public function exit():void
  {
  }

  public function get changeStateEventObject():EventObject //ChangeStateEvent
  {
    return this._changeStateEventObject;
  }

  //<--IState

  private function editorCompleteHandler(event:EditorCloseEvent):void
  {
    this._editorView.closeEventObject.removeEventListener(editorCompleteHandler);
    switch (event.editorCloseEnum)
    {
      case EditorCloseEnum.selectModuleData:
        //TODO: Move this to using module data id or the module data pointer itself.
        //var index:int = this._moduleDatas.indexOf(this._editorView.selectedModuleData);
        this._changeStateEventObject.dispatchEvent(new ChangeStateEvent(this._changeStateEventObject.type, StateEnum.GoToExercise, this._editorView.selectedModuleData));
        break;
    }
  }
}
}